//
//  CCRTAPIClient.m
//  iSeal6100
//
//  Created by He Yuan on 13-1-24.
//  Copyright (c) 2013年 http://www.heyuan110.com. All rights reserved.
//

#import "APIClient.h"
#import "AFNetworkActivityIndicatorManager.h"
#import "AFJSONRequestOperation.h"

@implementation APIClient
@synthesize allErrorDict = _allErrorDict;
@synthesize protocolVersion = _protocolVersion;
@synthesize appVersion = _appVersion;

+ (APIClient *)sharedClient
{
    static APIClient *_sharedClient = nil;
    //创建一个单例
    static dispatch_once_t onceSingleton;
    
    dispatch_once(&onceSingleton, ^{
        _sharedClient = [[APIClient alloc] initWithBaseURL:[NSURL URLWithString:WEBSERVICE_URL]];
    });
    return _sharedClient;
}


- (id)initWithBaseURL:(NSURL *)url
{
    self = [super initWithBaseURL:url];
    
    if (!self)
    {
        return nil;
    }
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"APIErrorCodeList" ofType:@"plist"];
    
    _allErrorDict = [[NSMutableDictionary alloc]initWithContentsOfFile:filePath];

    _protocolVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"Protocol version"];
    
    _appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    
    [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];

    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    
	[self setDefaultHeader:@"Accept" value:@"application/json"];

    self.parameterEncoding = AFJSONParameterEncoding;
    
    return self;
}

- (void)postPath:(NSString *)path
      parameters:(NSDictionary *)parameters
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];
    [super postPath:path parameters:parameters success:success failure:failure];
}

+(NSString *)getPath:(NSDictionary *)value
{
    NSData *valuedata = [NSJSONSerialization dataWithJSONObject:value options:0 error:nil];
    
    NSString *vString = [[NSString alloc]initWithData:valuedata encoding:NSUTF8StringEncoding];
    
    NSString *path = [NSString stringWithFormat:@"?%@=%@&protocolv=%@&appv=%@",REQ_JSON_KEY,vString,[APIClient sharedClient].protocolVersion,[APIClient sharedClient].appVersion];
    
    return path;
}

//是否有网络
+(BOOL)isNetWork
{
    [[APIClient sharedClient]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus netstatus){
    
        HYLog(@"netstatus--->%d",netstatus);

    }];
    
    AFNetworkReachabilityStatus status = [[APIClient sharedClient] networkReachabilityStatus];
    
    HYLog(@"%d",status);
    return  !(status == AFNetworkReachabilityStatusNotReachable);
}


- (NSString *)errorMessageWithErrorCode:(NSString *)error
{
    if(_allErrorDict != nil && error && [error length]>0)
    {
        NSString *errorCode = [_allErrorDict objectForKey:error];
//        if (errorCode == nil || [errorCode length] < 1)
//        {
//            errorCode = [_allErrorDict objectForKey:@"-404"];
//        }
        return errorCode;
    }
    return nil;
}


@end
